SUBROUTINE notmayordecision(t,pmwage,netwealth,ELNqq)
  USE commonvars
  USE Nelder_Mead
  IMPLICIT NONE  
  INCLUDE 'mpif.h'

  REAL(8), INTENT(IN)  :: netwealth, ELNqq, pmwage
  INTEGER, INTENT(IN)  :: t
  REAL(8)              :: pcons, netwealtht, newsav
  REAL(8), EXTERNAL    :: zeroin
  INTEGER, PARAMETER   :: nn = 2, nn0 = 1
  REAL(8), ALLOCATABLE :: xx0(:)
  REAL(8)              :: object0, simp0, step0(nn0), stopcr0, var0(nn0)
  INTEGER              :: iiiprint0, iquad0, maxf0, nloop0, ier0
  LOGICAL              :: first0
  EXTERNAL             :: objfcn_pmayor, utilfun

  waget = pmwage

  ! This variable represents the Expected log of public consumption produced by the current mayor (not the individual we consider)
  ELNqqt = ELNqq
  netwealtht = netwealth

  IF (t == Tend) THEN

     !new savings must be zero in the last period
     newsav = wealth(w0)
     totrest = waget*lsnotmayor + R*netwealtht - newsav

!!$     if (flag_here == 484) write(*,'(a,7f16.8)') 'totres,subpcons,waget, wealtht,newsav', totrest,subpcons,waget,netwealtht,newsav,ELNqqt,theta

     !check if the the amount of available resources is positive
     !if not, there is no solution
!!$     IF (totrest <= subpcons) THEN
     IF (totrest <= subpcons) THEN

!!$        pcons = subpcons + small_no
        pcons = subpcons
        CALL utilfun(pcons,ELNqqt,theta,utiloptpmg)

!!$        if (flag_here == 484) write(*,'(a,6f16.8)') 'inf:pcons,ELNqqt,util',totrest,netwealtht,pcons,ELNqqt,theta,utiloptpmg
        
        ! When we compute the value function we assign to the infeasibles a value that is a percentage of the lowest observed emax (in emaxfuntemp and emaxfun)
!!$        IF (flag_simulation == 0) utiloptpmg = -infty

!!$        if (savingt > saving(5)-small_no .AND. savingt < saving(5)+small_no) write(*,*) 'check in inf', pcons, ELNqqt, theta, utiloptpmg, totrest, waget*lsnotmayor, savingt, newsav, finet,  waget, lsnotmayor

!!$        if (flag_here  == 1) write(*,'(a,1i5,6f20.10)') 'check in subpcons', iwealth, pcons, ELNqqt, theta, netwealtht, totrest, subpcons
!!$        if (flag_here  == 1) write(*,*) 'utiloptpmg', utiloptpmg

!!$        if (flag_here  == 1) write(*,'(a,7f20.10)') 'check in 0', utiloptpmg, netwealtht, pcons, ELNqqt, theta, utiloptpmg, netwealtht

        RETURN
     END IF

     pcons = totrest

     CALL utilfun(pcons,ELNqqt,theta,utiloptpmg)

!!$     if (flag_here == 666) write(*,'(a,5F18.8)') 'totres,netW,pcons,ELNqqt,util',totrest,netwealtht,pcons,ELNqqt,utiloptpmg

!!$        if (savingt > saving(5)-small_no .AND. savingt < saving(5)+small_no) write(*,*) 'check NOT inf', pcons, ELNqqt, theta, utiloptpmg, totrest, waget*lsnotmayor, savingt, newsav, finet,

!!$     if (flag_here  == 1) write(*,'(a,5f20.10)') 'check in', utiloptpmg, netwealtht, pcons, ELNqqt, theta

     IF (utiloptpmg <  check_bignumber .OR. pcons <= 0d0) THEN
        write(*,*) 'util or pcons are negative in notmayordecision 1', utiloptpmg, pmwage, netwealtht, ELNqqt, newsav, totrest, pcons
        utiloptpmg = bignumber
     END IF

  ELSE

     totrest = waget*lsnotmayor + R*netwealtht

     ALLOCATE(xx0(nn0))
     ! step sizes                   
     step0 = 1d0
     ! max # of function evaluations
     maxf0 = 10000
     ! print every iteration        
     iiiprint0 = -1
     ! stopping criterion           
     stopcr0 = 1d-8
     nloop0 = 2*nn0
     ! don't fit a quadratic surface
     iquad0 = 0
     ! accuracy 9 digits          
     simp0 = 1d-9
     ! declare this is first time 
     first0 = .true.

     IF (totrest >= 0d0) THEN
        newsav = 0.1d0*totrest
     ELSE
        newsav = 1.1d0*totrest
     END IF
     xx0(1) = LOG((newsav - lbound_sav)/(totrest-newsav))

     IF (isnan(xx0(1))) THEN
        WRITE(*,*) 'Sol 0 isnan notmayor',isnan(xx0(1)),xx0(1), totrest, waget, netwealtht
        xx0(1) = LOG((netwealtht - lbound_sav)/(totrest-netwealtht))
     END IF

     vemaxoptpmg = -infty
     flag_sopt_sa = 0 
     CALL minim(xx0, step0, nn0, object0, maxf0, iiiprint0, stopcr0, nloop0, iquad0, simp0, var0, objfcn_pmayor, ier0)

!!$     IF (flag_fafb == 1) write(*,'(a,1i5,10F20.6)') 'totres,pcons,waget,netwealtht,newsav,ELNqqt,util,emax,vemax', ier0,totrest,xx0,pconsoptpmg,waget,netwealtht,newsavoptpmg,ELNqqt,utiloptpmg,emaxoptpmg,vemaxoptpmg
!!$     IF (ier0 == 1) write(*,'(a,10F20.6)') 'ier0:totres,pcons,waget,netwealtht,newsav,ELNqqt,util,emax,vemax', totrest,xx0,pconsoptpmg,waget,netwealtht,newsavoptpmg,ELNqqt,utiloptpmg,emaxoptpmg,vemaxoptpmg

!!$     !To generate the optimal values for all variables of interest
!!$     CALL pastmayorsolution(nn0, xx0, fval)

!!$     IF (myrank == 0) WRITE(*,'(a,6F20.10)') 'pm out newsav,newsteal,util,betadf*emaxp,vemax',totrest,newsavoptpmg,utiloptpmg,betadf*emaxoptpmg,pconsoptpmg,vemaxoptpmg
!!$     IF (myrank == 0) WRITE(*,*)''

!!$     if (flag_here == 1) write(*,'(a,9F16.8)') 'totres,pcons,waget,netwealtht,newsav,ELNqqt,util,emax,vemax', totrest,pconsoptpmg,waget,netwealtht,newsavoptpmg,ELNqqt,utiloptpmg,emaxoptpmg,vemaxoptpmg
!!$     if (flag_here == 1) write(*,*) 'pcons,ELNqqt,theta,util', pcons,ELNqqt,theta,utiloptpmg
!!$     if (flag_here == 1) write(*,*) 'betat', betat, MMg
     
     IF (utiloptpmg <  check_bignumber .OR. pconsoptpmg <= 0.0d0) THEN
        write(*,'(a,1f30.10,2f20.15)') 'util or pcons are negative in notmayordecision 2', utiloptpmg, pconsoptpmg, ELNqqt
     END IF

  END IF

!!$CONTAINS
!!$
!!$  FUNCTION focnotmayor(newsav)
!!$    USE commonvars
!!$    IMPLICIT NONE
!!$    REAL(8) :: focnotmayor, newsav, dvsav,  dum
!!$    
!!$    pcons = totrest-newsav
!!$
!!$    dVsav = 0.0d0
!!$    dum = 0.0d0
!!$    IF (newsav <= betat(MMg-2)) dum = 1d0
!!$    dVsav = betat(MMg-4)-2.0d0*betat(MMg-3)*dum*(betat(MMg-2)-newsav)
!!$    IF(newsav >= betat(MMg-1)) dVsav = (-1d0)*min_slope
!!$
!!$    focnotmayor = -pcons**(-gamma)+betadf*dVsav
!!$
!!$  END FUNCTION focnotmayor

END SUBROUTINE notmayordecision
